﻿using System;
using System.Net;
using System.Net.Mail;
using System.Web.UI;

namespace OSC
{
    public partial class smptTest : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblError.Text = "";
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                var objMail = new CustomeEmail();


                //Set the sender address of the mail message
                objMail.From = new MailAddress(txtFrom.Text.Trim());

                //Set the recepient address of the mail message
                objMail.To.Add(new MailAddress(txtTo.Text.Trim()));

                //set the subject of email message
                objMail.Subject = txtSub.Text.Trim();

                objMail.Body = txtBody.Text.Trim();


                //Set the format of mail message body as HTML
                objMail.IsBodyHtml = true;

                // Set the priority of the mail message to normal
                objMail.Priority = MailPriority.Normal;

                //Make a new instance of SMTP client

                var smtpErrorMailClient = new SmtpClient();
                smtpErrorMailClient.Host = txtServerName.Text.Trim();
                if (txtPort.Text.Trim() != "")
                {
                    smtpErrorMailClient.Port = Convert.ToInt32(txtPort.Text.Trim());
                }
                if (txtUserName.Text.Trim() != "")
                {
                    smtpErrorMailClient.Credentials = new NetworkCredential(txtUserName.Text.Trim(),
                                                                            txtPassword.Text.Trim());
                    smtpErrorMailClient.UseDefaultCredentials = false;
                }
                else
                {
                    smtpErrorMailClient.UseDefaultCredentials = true;
                }
                smtpErrorMailClient.Send(objMail);
                lblError.CssClass = "lblSucess";
                lblError.Text = "Sucess";
            }
            catch (Exception ex)
            {
                lblError.CssClass = "lblerror";
                lblError.Text = ex.Message;
            }
        }
    }
}